package com.xuchaotong.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by 357070 on 2017/4/5.
 */
public class RedisClient {

    private static RedisClient redisClient = new RedisClient();
    private Jedis jedis;
    private JedisPool jedisPool;
    private JedisPoolConfig config;

    private RedisClient() {
        if (jedisPool == null) {
            config = new JedisPoolConfig();
            config.setMaxTotal(10000);
            config.setMaxIdle(10000);
            config.setMaxWaitMillis(1000);
            config.setTestOnBorrow(true);

            jedisPool = new JedisPool(config, "127.0.0.1", 6379);
        }
    }


    public static synchronized RedisClient getInstance() {
        if (redisClient == null) {
            redisClient = new RedisClient();
        }

        return redisClient;
    }

    public Jedis getJedis() {
        return jedisPool.getResource();
    }


    public void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }


}
